package gonggong;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import util.Constant;
import util.TotalClass;
import util.UtilDAO;

import com.SmsModule.bean.TbSmsSend;
import com.SmsModule.dao.TbSmsSendDAO;
import com.dianbo.bean.Dianbo;
import com.dianbo.bean.SendDianbo;
import com.dianbo.dao.DianboDao;
import com.jifen.bean.JiFen;
import com.jifen.dao.JifenDao;
import com.order.bean.TbOrder;
import com.order.dao.TbOrderDao;

public class SanDianboDao {

	private ResultSet rs = null;
	private String sql = "";
	private List<SendDianbo> sendDianboList = null;
	public void operator(){
		List<SendDianbo> sendDianboList = this.getSendDianbo();
		if(sendDianboList!=null&&sendDianboList.size()>0){
			boolean bo = this.sendSms(sendDianboList);
			if(bo){
				String dianboId = "";
				for(SendDianbo dianbo:sendDianboList){
					if(dianboId.equals("")){
						dianboId+=dianbo.getDianboId();
					}else{
						dianboId+=","+dianbo.getDianboId();
					}
				}
				boolean boo= new DianboDao().modifyState(dianboId);
			}
			
		}
	}
	/**
	 *@author qingyu zhang
	 *@function:得到状态为0的要点播的信息
	 *2011-4-12
	 *下午09:11:56
	 *zzClub
	 *gonggong
	 */
	public List<SendDianbo> getSendDianbo(){
		String tiaojian="state = 0";
		sql = UtilDAO.getList("tb_send_dianbo", "", tiaojian);
		rs = new TotalClass().getResult(sql);
		try {
			while(rs.next()){
				if(sendDianboList==null){
					sendDianboList = new ArrayList<SendDianbo>();
				}
				sendDianboList.add(this.getSendDianbo(rs));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendDianboList;
	}
	public SendDianbo getSendDianbo(ResultSet rs){
		SendDianbo sendDianbo = new SendDianbo();
		try {
			sendDianbo.setDianboId(rs.getInt("dianboId"));
			sendDianbo.setSendPhone(rs.getString("sendPhone"));
			sendDianbo.setSmscontent(rs.getString("smscontent"));
			sendDianbo.setState(rs.getInt("state"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendDianbo;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:把取到的数据进行发送
	 * @param sendDianboList
	 *2011-4-12
	 *下午09:24:53
	 *zzClub
	 *gonggong
	 *void
	 */
	public boolean sendSms(List<SendDianbo> sendDianboList){
		boolean bo = false;
		List<TbSmsSend> smsSendList = new ArrayList<TbSmsSend>();
		if(sendDianboList!=null&&sendDianboList.size()>0){
			for(SendDianbo dianbo:sendDianboList){
				String smscontent = dianbo.getSmscontent();
				String lastSmscontent = smscontent;//.substring(2).trim().toLowerCase();//指令  ym(幽默)
				String phone = dianbo.getSendPhone();
				TbSmsSend sms = this.getSms(lastSmscontent, phone);
				if(sms!=null){
					smsSendList.add(sms);
					
				}
			
			}
		}
		if(smsSendList!=null&&smsSendList.size()>0){
			bo = new TbSmsSendDAO().addSmsList(smsSendList);
		}
		return bo ;
	}
	/**
	 * 得到此点播类型的积分
	 *@author qingyu zhang
	 *@function:
	 * @param typeId
	 * @param num
	 * @return
	 *2011-4-14
	 *上午09:48:53
	 *zzClub
	 *gonggong
	 *JiFen
	 */
	public JiFen getJifenDianbo(String typeId,int num){
		String condition="typeId='"+typeId+"' and integralType=1";
		JiFen jifen = null;//new JiFen();
		jifen = new JifenDao().getJiFen("tb_integral", "", condition);
		return jifen;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:根据指令得到其点播下的所有信息中的一条记录
	 * @param orderName
	 * @return
	 *2011-4-12
	 *下午09:31:14
	 *zzClub
	 *gonggong
	 *TbSmsSend
	 */
	public TbSmsSend getSms(String orderName,String phone){
		TbSmsSend sms = null;
		String condition ="orderValue='"+orderName+"' and orderType=0";
		List<TbOrder> orderList = new TbOrderDao().getOrderList("tb_order", "", condition);
		if(orderList!=null&&orderList.size()>0){
			TbOrder order = orderList.get(0);
			String typeId = order.getTypeId();
			String conditionT = "typeId='"+typeId+"'";
			List<Dianbo> dianboList = new ArrayList<Dianbo>();
			dianboList = new DianboDao().getDianboList("tb_qs_info","qsUpTime",conditionT);
			int num =0;
			if(dianboList!=null&&dianboList.size()>0){
				num =new Random().nextInt(dianboList.size());
			}
			 
			if(dianboList!=null&&dianboList.size()>0){
				sms = new TbSmsSend();
				Dianbo dianbo = dianboList.get(num);
				sms.setCreateTime(Constant.decodeDate());
				sms.setDestAdd(phone);
				sms.setIsSendTime(0);
				sms.setSendTime(Constant.decodeDate());
				sms.setSmsContent(dianbo.getQsContent());
				sms.setSmsSendState(0);
				sms.setUserId(0);
				sms.setSeriseId("0");
				
				//加用户点播的积分
				JiFen jifen = getJifenDianbo(typeId,1);
				String fenSh = "0";
				if(jifen!=null){
					fenSh= jifen.getIntegralValue();
				}
				boolean bo = new GetJifen().saveJifenToCustomer(phone,fenSh);
			}
			
		}
		return sms;
	}
}
